<?php
/**
 *
 * @author ninetyseven.
 * @date 2023-03-27 22:53
 */

namespace App\Services;


use App\Enum\CardEnum;
use App\Enum\OrderEnum;
use App\Enum\VehicleConfigEnum;
use App\Enum\WalletsEnum;
use App\Models\VehicleConfigModel;
use Illuminate\Support\Facades\Log;

class VehicleConfigService extends BaseService
{

    /**
     * 订单详情的查看
     *
     * @param $result
     * @param $distance
     * @return array
     */
    public static function _formatAmountAndRule($result,$distance): array
    {

        $kmNum = round($distance / 1000, OrderEnum::ORDER_DISTANCE_PRECISE_LENGTH);
        $startPrice = $result['start_price'];
        $startKM = $result['start_km'];
        $overPrice = $result['over_distince_price'];
        $overDistance = 0;

        if ($kmNum - $startKM > 0) {
            $overDistance = round($kmNum - $startKM, OrderEnum::ORDER_DISTANCE_PRECISE_LENGTH);
            $amount = floor($startPrice + $overDistance * $overPrice);
        } else $amount = $startPrice;

        $result['over_price'] = $amount - $startPrice;
        $result['over_distance_km'] = $overDistance;

        $result['over_distance_price'] = $result['over_distince_price']; unset($result['over_distince_price']);

        return [
            'amount' => $amount,
            'rule' => $result
        ];
    }
    /**
     * 金额计算 20230810 by LC
     * 未传递平台来源，默认使用捐赠者
     *
     * @param string $cityCode
     * @param int $distance
     * @param int $vehicleUseType
     * @param int $vehicleType
     * @param int $payment
     * @param bool $withRule
     * @return array|mixed
     * @throws \Exception
     */
    public static function amount(string $cityCode, int $distance, int $vehicleUseType, int $vehicleType, int $payment=OrderEnum::ORDER_PAY_GYCX_AXD, bool $withRule=false)
    {
        try {
            if (empty($distance)) {
                throw new \Exception('公里数不能为空');
            }

            $result = self::_platformAmountRule($cityCode, self::getPlatformId(), $vehicleUseType, $vehicleType, $payment);

            return self::_amountOrRule($result->toArray(), $distance, $payment, $withRule);
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * @throws \Exception
     */
    public static function amountAndRule($cityCode, int $kmNum, int $vehicleUseType, int $vehicleType, $payment=OrderEnum::ORDER_PAY_GYCX_AXD)
    {
        return self::amount($cityCode,$kmNum,$vehicleUseType,$vehicleType,$payment,true);
    }

    /**
     * 传递平台ID的算法
     *
     * @param int $platformId
     * @param string $cityCode
     * @param int $distance
     * @param int $vehicleUseType
     * @param int $vehicleType
     * @param int $payment
     * @return array|mixed
     * @throws \Exception
     */
    public static function platformAmount(int $platformId, string $cityCode, int $distance, int $vehicleUseType, int $vehicleType, int $payment=OrderEnum::ORDER_PAY_GYCX_AXD){
        try {
            if (empty($distance)) {
                throw new \Exception('公里数不能为空');
            }
            $result = self::_platformAmountRule($cityCode, $platformId, $vehicleUseType, $vehicleType, $payment);

            return self::_amountOrRule($result->toArray(), $distance, $payment);

        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
    //---------------------------------------
    //                     私有方法 bellow
    //---------------------------------------
    private static function _amountOrRule($result, $distance, $payment, bool $withRule=false)
    {
        $data = self::_formatAmountAndRule($result,$distance);

        Log::notice('km=' . $distance . ' .. amount=' . $data['amount']);

        if( $withRule ) return $data;
        else return $data['amount'];
    }
    private static function _platformAmountRule(string $cityCode, int $platformId, int $vehicleUseType, int $vehicleType, int $payment=OrderEnum::ORDER_PAY_GYCX_AXD){

        $fields = [
            'city_code','city_name','use_type','vehicle_type','start_price','over_distince_price','start_km','up_stream','id'
        ];
        $result = VehicleConfigModel::query()->where(['up_stream'=>$platformId, 'city_code'=>$cityCode,'vehicle_type'=>$vehicleType,'use_type'=>$vehicleUseType,'payment'=>$payment])->select($fields)->first();
        // 若取不到相应配置，报错
        if (empty($result)) {
            Log::error(sprintf("无法获取用车配置'up_stream'=%d, 'city_code'=%s,'vehicle_type'=%d,'use_type'=%d,'payment'=%d", $platformId,$cityCode,$vehicleType,$vehicleUseType,$payment));
            throw new \Exception('无法获取用车配置。');
        }
        return $result;
    }


}
